/**
 * Copyright (c) 2000-2011 Liferay, Inc. All rights reserved.
 *
 * This library is free software; you can redistribute it and/or modify it under
 * the terms of the GNU Lesser General Public License as published by the Free
 * Software Foundation; either version 2.1 of the License, or (at your option)
 * any later version.
 *
 * This library is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
 * details.
 */

package doan.quanlycanbo.portlet.service.impl;

import java.util.List;

import com.liferay.counter.service.CounterLocalServiceUtil;
import com.liferay.portal.kernel.exception.PortalException;
import com.liferay.portal.kernel.exception.SystemException;
import com.liferay.portal.model.ResourceConstants;

import doan.quanlycanbo.portlet.NoSuchhocviException;
import doan.quanlycanbo.portlet.model.hocvi;
import doan.quanlycanbo.portlet.service.base.hocviLocalServiceBaseImpl;

/**
 * 
 * @author Nguyen Tuan Vinh
 */
public class hocviLocalServiceImpl extends hocviLocalServiceBaseImpl {
	
	public List<hocvi> getAll() throws SystemException{
		return hocviPersistence.findAll();
	}
	
	public hocvi addHocVi(hocvi hocvi1, long userId) throws SystemException{
		long mahocvi = CounterLocalServiceUtil.increment(hocvi.class.getName());
		hocvi hocvi2 = hocviPersistence.create(mahocvi);
		hocvi2.setGroupId(hocvi1.getGroupId());
		hocvi2.setCompanyId(hocvi1.getCompanyId());
		hocvi2.setLanguageId(hocvi1.getLanguageId());
		hocvi2.setTenhocvi(hocvi1.getTenhocvi());

		try {
			resourceLocalService.addResources(hocvi1.getCompanyId(), hocvi1.getGroupId(), userId, 
					hocvi.class.getName(), hocvi2.getPrimaryKey(), false, true, true);
		} catch (PortalException e) {
			e.printStackTrace();
		}
		
		return hocviPersistence.update(hocvi2, false);
	}
	
	public hocvi updateHocVi(long mahocvi, String tenhocvi) throws SystemException, NoSuchhocviException{
		hocvi hocVi = hocviPersistence.findByPrimaryKey(mahocvi);
		hocVi.setTenhocvi(tenhocvi);
		return hocviPersistence.update(hocVi, false);
	}
	
	public hocvi deleteHocVi(long mahocvi) throws SystemException, NoSuchhocviException,PortalException{
		hocvi hocVi = hocviPersistence.findByPrimaryKey(mahocvi);
		return deleteHocVi(hocVi);
	}
	
	public hocvi deleteHocVi(hocvi hocVi) throws SystemException,PortalException{
		resourceLocalService.deleteResource(hocVi.getCompanyId(), hocvi.class.getName(), ResourceConstants.SCOPE_INDIVIDUAL, hocVi.getPrimaryKey());
		return hocviPersistence.remove(hocVi);
	}
}